<template>
    <div class="forest-main">
        <div class="forest-content">
            <div class="days" v-for="(day, index) in weatherData" :key="index" :style="{ color: index === 0 ? '#51F1FF' : '#ffffff' }">
                <div>{{ day.weekDay }}</div>
                <div>{{ day.showDate }}</div>
                <div>{{ day.weathertext12 }}</div>
                <div>
                    <img :src="getImgSrc(day.weathertext12)" alt="">
                </div>
                <div>{{ day.minTemp }}</div>
                <div>{{ day.maxtemp }}</div>
                <div>{{ day.windd12 }}</div>
                <div>{{ day.winds12 }}</div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getCountryStation7DayForecast } from '@/api/index'
import moment from 'moment'

const weatherData = ref([])

onMounted(() => {
    getData()
});

function getWeekDay(date) {
  const weekday = moment(date).format('dddd');
  return weekday;
}

const getImgSrc = (src) => {
  return new URL(`../../../assets/images/weather/day/${src}.svg`, import.meta.url).href
}


function getChineseWeekDay(date) {
  const daysOfWeek = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  return daysOfWeek[date.getDay()];
}

function getData() {
    let params = {
        stationNum: '53698',
    }
    getCountryStation7DayForecast(params).then((res) => {
        let obj = res.data.obj
        weatherData.value = obj[params.stationNum]
        weatherData.value.forEach((item,index) => {
            if (index === 0) {
                item.weekDay = '今天'
            } else if (index === 1) {
                item.weekDay = '明天'
            } else {
                item.weekDay = getChineseWeekDay(new Date(item.time))
            }

            item.showDate = moment(item.time).format('MM/DD')
            let minTemp = item.temp24,maxtemp = item.temp12
            if (Number(minTemp) > Number(item.temp12)) {
                minTemp = item.temp12
                maxtemp = item.temp24
            }
            item.minTemp = minTemp
            item.maxtemp = maxtemp
        })
    })
}

defineExpose({ getData })

</script>
<style lang="scss" scoped>
.forest-main {
    width: 588px;
    height: 337px;
    background: url('@/assets/images/weather/七天预报背景.png');
    display: flex;
    justify-content: center;
    .forest-content {
        padding-top: 70px;
        display: flex;
        flex-direction: row;
        .days {
            display: flex;
            flex-direction: column;
            align-items: center;
            color: white;
            margin-right: 25px;
            div {
                margin-bottom: 5px;
                img {
                    width: 40px;
                }
            }
        }
    }
}
</style>
